package com.ruoyi.web.controller.authentication;


import cn.hxzy.common.CertificationStatus;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxzy.authentication.domain.TsDriversLicenseCertification;
import com.hxzy.authentication.domain.vo.DriversVo;
import com.hxzy.authentication.service.TsDriversLicenseCertificationService;
import com.hxzy.user.entity.TmUser;
import com.hxzy.user.service.TmUserService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mingqian.Wu
 * @since 2023-06-23
 */
@DataSource(DataSourceType.AUTHENTICATION)
@Controller
@RequestMapping("/system/certification")
public class TsDriversLicenseCertificationController {
    @Autowired
    private TmUserService tmUserService;

    @Autowired
    private TsDriversLicenseCertificationService tsDriversLicenseCertificationService;

    @PostMapping("/getList/{current}/{limit}")
    @Anonymous
    public AjaxResult getList(
            @PathVariable Long current,
            @PathVariable Long limit,
            @RequestBody DriversVo driversVo
            ) {

        Page<TsDriversLicenseCertification> page = new Page<>(current, limit);

        tsDriversLicenseCertificationService.selectPage(page, driversVo);

        return AjaxResult.success(page);
    }

    /**
     * 根据驾驶证审核id查询驾驶证审核信息
     * @param authenticationId
     * @return
     */
    @Anonymous
    @GetMapping("/{authenticationId}")
    public AjaxResult getById(@PathVariable String authenticationId){
        TsDriversLicenseCertification byId = tsDriversLicenseCertificationService.getById(authenticationId);

        if (byId != null) {
            return AjaxResult.success(byId);
        }
        return AjaxResult.error();
    }

    /**
     * 根据驾驶证审核id查询驾驶证审核信息
     * @param userId
     * @return
     */
    @Anonymous
    @GetMapping("/getDrivingLicenceInfo/{userId}")
    public AjaxResult getDrivingLicenceInfo(@PathVariable String userId){
        TsDriversLicenseCertification byId = tsDriversLicenseCertificationService.getById(userId);

        if (byId != null) {
            return AjaxResult.success(byId);
        }
        return AjaxResult.error();
    }

    /**
     * 驾驶证审核
     * @param tsDriversLicenseCertification
     * @return
     */
    @Anonymous
    @PutMapping
    public AjaxResult drivingLicenceAudit(@RequestBody TsDriversLicenseCertification tsDriversLicenseCertification){
        //修改驾驶证审核表的审核状态
        boolean flagA = tsDriversLicenseCertificationService.updateById(tsDriversLicenseCertification);

        if (flagA) {
            TmUser tmUser = new TmUser();
            tmUser.setUserId(tsDriversLicenseCertification.getUserId());
            tmUser.setDriver(CertificationStatus.certificationStatus_03.getCertificationStatus());
            //修改用户表的司机认证状态
            boolean flagB = tmUserService.updateById(tmUser);
            if (flagB) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error("该用户不存在");
            }
        }

        return AjaxResult.error();
    }
}

